package cn.edu.tju.ldj.analytics.utils;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.apache.log4j.Logger;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import cn.edu.tju.ldj.analytics.Factory;

@SuppressWarnings("unchecked")
public class IXmlUtil {
	@SuppressWarnings("unused")
	private static Logger logger;
	static {
		logger = Logger.getLogger(IXmlUtil.class.getName());
	}

	private String getXmlFileName(String fileName) {
		String paramFilename = IXmlUtil.class.getClassLoader().getResource(
				fileName).getFile();
		return paramFilename;
	}

	private Element getXmlRoot(String fileName) throws Exception {
		String xmlFileName = getXmlFileName(fileName);
		SAXBuilder builder = new SAXBuilder();
		Document doc = builder.build(new java.io.File(xmlFileName));
		Element root = doc.getRootElement();
		return root;
	}

	public Object loadObject(String fileName, String childName)
			throws Exception {
		Element root = this.getXmlRoot(fileName);
		Object obj = elementToObject(root.getChild(childName), null);
		return obj;
	}

	@SuppressWarnings("rawtypes")
	private Object elementToObject(Element root, String className)
			throws ClassNotFoundException, InstantiationException,
			IllegalAccessException, SecurityException,
			IllegalArgumentException, NoSuchMethodException,
			InvocationTargetException {
		if (null == root) {
			return null;
		}
		if (Factory.utilStr.isEmpty(className)) {
			className = root.getAttributeValue("class");			
		}

		// default String
		if (Factory.utilStr.isEmpty(className)) {
			return root.getTextTrim();
		} else if ("Integer".equals(className)) {
			return new Integer(root.getTextTrim());
		} else if ("Long".equals(className)) {
			return new Long(root.getTextTrim());
		} else if ("Double".equals(className)) {
			return new Double(root.getTextTrim());
		} else if ("Boolean".equals(className)) {
			return new Boolean(root.getTextTrim());
		} else if ("Date".equals(className)) {
			return Factory.utilStr.strToDate(root.getTextTrim(),
					"yyyyMMdd HH:mm:ss");
		}
		Object obj = Factory.utilReflect.loadInstance(className);

		Element dataSource=root.getChild("data_source");
		if (dataSource != null) {
//			String method = dataSource.getChildTextTrim("method");
//			obj = Factory.utilReflect.invoke(Factory.servDataList, method);
		} else {
			// TODO 数组

			List<Element> elements = root.getChildren();
			if (obj instanceof java.util.Set) {
				for (int i = 0; i < elements.size(); i++) {
					Object child = elementToObject(elements.get(i), null);
					((java.util.Set) obj).add(child);
				}
			} else if (obj instanceof java.util.List) {
				for (int i = 0; i < elements.size(); i++) {
					Object child = elementToObject(elements.get(i), null);
					((java.util.List) obj).add(child);
				}
			} else if (obj instanceof java.util.Map) {
				for (int i = 0; i < elements.size(); i++) {
					Object key = elementToObject(elements.get(i)
							.getChild("key"), null);
					Object value = elementToObject(elements.get(i).getChild(
							"value"), null);
					((java.util.Map) obj).put(key, value);
				}
			} else {
				for (int i = 0; i < elements.size(); i++) {
					Object child = elementToObject(elements.get(i), null);
					String beanId = elements.get(i).getAttributeValue("propName");
					if (Factory.utilStr.isEmpty(beanId)) {
						beanId = elements.get(i).getName();
					}
					if(beanId.equalsIgnoreCase("components")){
						System.out.println("ok");
					}
					Factory.utilSys.ASSERT(!Factory.utilStr.isEmpty(beanId));
					// System.out.println(obj+" "+beanId+" "+child);
					Factory.utilReflect.setField(obj, beanId, child);
					// System.out.println(obj+" "+beanId+" "+child+" ok");
				}
			}
		}
		return obj;
	}

}
